/*
 * @Author: tcly861204 356671808@qq.com
 * @Date: 2023-08-24 08:53:39
 * @LastEditors: tcly861204
 * @LastEditTime: 2023-08-24 08:55:41
 */
function downloadFile(content: any, filename: any) {
  var a = document.createElement('a')
  var blob = new Blob([content])
  var url = window.URL.createObjectURL(blob)
  a.href = url
  a.download = filename
  a.click()
  window.URL.revokeObjectURL(url)
}


function ajax(url: any, callback: any, options: any) {
  window.URL = window.URL || window.webkitURL
  var xhr = new XMLHttpRequest()
  xhr.open('get', url, true)
  if (options.responseType) {
    xhr.responseType = options.responseType
  }
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 && xhr.status === 200) {
      callback(xhr)
    }
  }
  xhr.send()
}

const download = (url: any, filename: any) => {
  ajax(url, function(xhr: any) {
    downloadFile(xhr.response, filename)
  }, {
    responseType: 'blob'
  })
}

export default download
